package com.qianfeng.openapi.web.master.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.openapi.web.master.feign.CacheFeign;
import com.qianfeng.openapi.web.master.mapper.AppInfoMapper;
import com.qianfeng.openapi.web.master.pojo.ApiSystemParam;
import com.qianfeng.openapi.web.master.pojo.AppInfo;
import com.qianfeng.openapi.web.master.service.AppInfoService;
import com.qianfeng.openapi.web.master.service.CustomerService;
import com.qianfeng.openplatform.common.SystemParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author LJF
 * @Date 2020/7/1 8:07
 * @Version 1.0
 */
@Service
@Transactional
public class AppInfoServiceImpl implements AppInfoService {

    @Autowired
    private AppInfoMapper appInfoMapper;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private CacheFeign cacheFeign;


    @Override
    public int insertAppInfo(AppInfo appInfo) {

        appInfo.setCorpName(customerService.getCustomerById(appInfo.getCusId()).getNickname());
        int i = appInfoMapper.insertAppInfo(appInfo);

        try {
            cacheFeign.hMset(SystemParams.APPKEY_REDIS_PRE+appInfo.getAppKey(),appInfo);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return i;
    }

    @Override
    public int updateAppInfo(AppInfo appInfo) {
        appInfo.setCorpName(customerService.getCustomerById(appInfo.getCusId()).getNickname());
        int i = appInfoMapper.updateAppInfo(appInfo);
        try {
            cacheFeign.hMset(SystemParams.APPKEY_REDIS_PRE+appInfo.getAppKey(),appInfo);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return i;
    }

    @Override
    public int deleteAppInfo(Long id) {
        return appInfoMapper.deleteAppInfo(id);
    }

    @Override
    public AppInfo getAppInfoById(Long id) {
        return appInfoMapper.getAppInfoById(id);
    }

    @Override
    public List<AppInfo> getAllAppInfos() {
        return appInfoMapper.getAllAppInfos();
    }

    @Override
    public PageInfo<AppInfo> getCustomerByPage(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        return new PageInfo<>(appInfoMapper.getAllAppInfos());
    }

    @Override
    public void deleteAppInfos(Long[] ids) {
        if (ids!=null || ids.length==0) {
            return;
        }
        for (Long id : ids) {
            AppInfo appInfoById = appInfoMapper.getAppInfoById(id);
            int i = appInfoMapper.deleteAppInfo(id);
            if (i==1 && appInfoById!=null){
                try {
                    cacheFeign.deleteKey(SystemParams.APPKEY_REDIS_PRE+appInfoById.getAppKey());
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            }
        }
    }

    @Override
    public PageInfo<AppInfo> selectAppInfoByCondition(Integer page, Integer limit, String appName, Integer state) {
        PageHelper.startPage(page,limit);
        List<AppInfo> appInfos = appInfoMapper.selectAppInfoByCondition(appName, state);
        return new PageInfo<>(appInfos);
    }
}
